﻿//
//  GNS_ClientLogin.cpp
//  MobileManage
//
//  Created by mr.l on 16/1/23.
//
//
#include "GnsClientFrame.h"
#include "GNS_ClientLogin.h"
#include "GNS_Common.h"
#include "Enum.h"
#include "GNS_LoginScene.h"
#include "GNS_PlayerMgr.h"
#include "GNS_SceneMgr.h"
#include "GNS_HallScene.h" 
#include "CMD_NewGameHall.h"
#include "GNS_ClientHall.h"

USING_NS_CC;

extern GNS_LoginScene* _loginScene;


GNS_ClientLogin::GNS_ClientLogin()
{
	
}

bool GNS_ClientLogin::OnHandleSocketMsg(int mainCmd, int subCmd, unsigned char*msgData, int dataLen)
{
	switch (mainCmd)
	{
		case MAIN_MSG_LOGIN: // 登录消息
		{
			HandlePacketLogin(mainCmd, subCmd, msgData, dataLen);
			 break;
		} 
		case MAIN_MSG_SYS:
		{
			HandleGetIsAgent(mainCmd, subCmd, msgData, dataLen);
			break;
		}
		default:
		{
			log("HandlePacketRaw.MainCode[%d].Unknown!", mainCmd);
			break;
		}

     }
    return true;
}

void GNS_ClientLogin::HandleGetIsAgent(int mainCmd,int subCode, unsigned char *pData/*=NULL*/, int pDataLen/*=0*/)
{
	GnsClientFrame* pGnsClientFrame = GnsClientFrame::getInstance();
	assert(pGnsClientFrame);
	
	int iRes = 0;

	IEventBridge::EVENT_INFO eventInfo;
	eventInfo.iMainCode = mainCmd;
	eventInfo.iSubCode = subCode;
	iRes = eventInfo.copyToBody((char*)pData, pDataLen);
	//assert(0 == iRes);
	if (0 == iRes)
		IEventBridge::getInstance()->pushBack(eventInfo);
	switch (subCode)
	{
	case SUB_SS_GET_IS_AGENT:
	{
		CMD_IsAgent *pIsAgent = (CMD_IsAgent*)pData;
		if (!pIsAgent)
			return;
		//SIX_GlobalMgr::GetInstance()->SetisSetInviter(pIsAgent->iIsAgent);
		//Disconnect();//获取数据后断开

		break;
	}
	case SUB_CSG_CONTACT_INFORMATION: //与官方的联系方式
	{
		
		log("closeSocket LOGINSERVER");
		CMD_ContactInformation *pContactInfo = (CMD_ContactInformation *)pData;
		if (!pContactInfo || pDataLen != sizeof(CMD_ContactInformation))
			return;
		GNS_PlayerMgr::GetInstance()->SetContactInfo(*pContactInfo);

		break;
	}
	default:
	{
			   break;
	}
	}
}

void GNS_ClientLogin::HandlePacketLogin(int mainCmd, int subCode, unsigned char *pData, int pDataLen)
{
	GnsClientFrame* pGnsClientFrame = GnsClientFrame::getInstance();
	assert(pGnsClientFrame);
	
	int iRes = 0;

	IEventBridge::EVENT_INFO eventInfo;
	eventInfo.iMainCode = mainCmd;
	eventInfo.iSubCode = subCode;
	iRes = eventInfo.copyToBody((char*)pData, pDataLen);
	//assert(0 == iRes);
	if (0 == iRes)
		IEventBridge::getInstance()->pushBack(eventInfo);

	switch (subCode)
	{
		// 版本更新
		case SUB_SG_VERSION_REEOR:
		{
			CMD_VerError * pverError = (CMD_VerError*)pData;
			log("服务器回：  版本错误！Version =%d ", pverError->iVersion);
			break;
		}
			// 登录成功
		case SUB_SC_USER_LOGIN_SUC:
		{
			CMD_SC_UserLoginInfo *pck = (CMD_SC_UserLoginInfo*)pData;
			log("pck.iRet[%d]", pck->iRet);
			 // 成功
			if (pck->iRet == RET_LOGIN_OPERATE_SUCCEED)
			{
				pGnsClientFrame->sendMsg(LOGINSERVER, MAIN_MSG_SYS, SUB_CSG_CONTACT_INFORMATION, 0, 0);
				GNS_PlayerMgr::GetInstance()->setMyUseridx(pck->llUserID);
				GNS_PlayerMgr::GetInstance()->setMyUserInfo(pck->szName, GNS_PlayerMgr::GetInstance()->getMyPassOgi(), GNS_PlayerMgr::GetInstance()->getMyPassMD5(), "");
				GNS_PlayerMgr::GetInstance()->setMyNickName(pck->szNickName);
				GNS_PlayerMgr::GetInstance()->setMyGameGold(pck->llGold);
				GNS_PlayerMgr::GetInstance()->setMyExperienceGold(pck->llExperienceGold);
				GNS_PlayerMgr::GetInstance()->setMyLogoID(pck->lSysLogoID);
				GNS_PlayerMgr::GetInstance()->setMyCellPhone(pck->szCellPhone);
				//SIX_PlayerMgr::GetInstance()->setMyLostCount(pck->iLostCount);
				//SIX_PlayerMgr::GetInstance()->setMyWinCount(pck->iWinCount);
				//SIX_PlayerMgr::GetInstance()->setMyMaxWinGold(pck->iMaxWinGold);
				//SIX_PlayerMgr::GetInstance()->setMyMaxCardData(pck->lMaxCardData);
				GNS_PlayerMgr::GetInstance()->setAgentID(pck->iAgentId);
				GNS_PlayerMgr::GetInstance()->setRecommendCode(pck->nRecommendCode);
			}
			break;
		}
	}
}

bool GNS_ClientLogin::OnHandleUIMsg(const int &msgCmd, const void *msgData)
{
	switch (msgCmd) {
        case LOAD_FINISH:
        {
            
            break;
        }
        default:
            break;
    }
    return true;
}
PopWndLayer * GNS_ClientLogin::OnUIAction(std::string Text,bool Effect)
{
    pPopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
    pPopWnd->setClickWndOutsideClose(true);
    pPopWnd->SetTitle("提示!");
    pPopWnd->SetContentText(Text, Color3B::BLACK);
    pPopWnd->PopWndEffectseffects("RunCircle.plist",Effect);
    return pPopWnd;
}

ConnectionType GNS_ClientLogin::GetConnectionType()
{
	return m_ConnectionType;
}

void GNS_ClientLogin::SetConnectionType(ConnectionType ctType)
{
	m_ConnectionType = ctType;
}

//void GNS_ClientLogin::connect(std::string ip, unsigned short part)
//{
// 	_LoginSock->connectWithCallback(ip.c_str(), part, [this](bool result)
// 	{
// 		if (result) {
// 			m_isConnect = result;
// 				switch (GetConnectionType())
// 				{
// 				case CONNECTION_LOGIN:
// 				{
// 					break;
// 				}
// 				case CONNECTION_GET_IS_AGENT://获取注册是否需要推荐人.
// 				{
// 												 // OnSendGetIsAgent();
// 												 break;
// 				}
// 				case CONNECTION_STARTER_LOGIN://启动器启动
// 				{
// 												  //  Starterlogin();
// 												  break;
// 				}
// 				case CONNECTION_GET_CONTACT_INFO://获取官方联系方式 
// 				{
// 					sendData(MAIN_MSG_SYS, SUB_CSG_CONTACT_INFORMATION, 0, 0);
// 					break;
// 				}
// 			}
// 		}
// 		else
// 		{
// 			m_isConnect = false;
// 			_loginScene->ShowLoginSceneTips("服务器连接失败", true);
// 		}});
//}